﻿using UnityEngine;
using System.Collections;

public class HireApply : MonoBehaviour
{

    GameObject Check;
    GameObject FileCover;
    GameObject classArrowPrefab;

    public bool bFileCoverOn;
    bool bBtnDisabled;

    // Use this for initialization
    void Awake()
    {
        bBtnDisabled = false;
        bFileCoverOn = false;
        Check = Resources.Load("Prefaps/UI/StaticUI/Check/Check") as GameObject;
        FileCover = Resources.Load("Prefaps/UI/StaticUI/FileCover/FileCover") as GameObject;
        classArrowPrefab = Resources.Load("Prefaps/UI/StaticUI/ClassArrow/ClassArrows") as GameObject;
    }

    void Start()
    {
        GetComponent<UISprite>().depth = transform.parent.GetComponent<UISprite>().depth + 1;
        transform.FindChild("Label").GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void DestroyThis()
    {
        Destroy(gameObject);
    }

    void OnClick()
    {
        GameObject.Find("MainCamera").GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Pencil_Check"));

        if (!bBtnDisabled && GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum != 99)
        {
            if (GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum != 6 && transform.childCount == 1 && bFileCoverOn)
            { // Make Unit!

                UnitData CurSelUnit = GameObject.Find("ShopWdw(Clone)").GetComponent<HireWindow>().m_HireList[GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum];

                if (GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney >= CurSelUnit.ClassStatus.AssnStatus.iAssnTrainPrice)
                { //have enough money

                    (Instantiate(Check) as GameObject).transform.parent = transform;
                    transform.FindChild("Check(Clone)").GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
                    transform.FindChild("Check(Clone)").transform.localPosition = new Vector3(1.25f, 2.5f);
                    transform.FindChild("Check(Clone)").transform.localScale = Vector3.one;

                    transform.FindChild("Label").GetComponent<UILabel>().text = "HIRE";
                    bFileCoverOn = false;


                    GameObject.Find("ShopWdw(Clone)").GetComponent<HireWindow>().m_HireApplyList.Add(CurSelUnit);
                    GameObject.Find("SceneDataMgr").GetComponent<SceneDataMgr>().g_iMoney -= CurSelUnit.ClassStatus.AssnStatus.iAssnTrainPrice;

                    GameObject.Find("FileCover(Clone)").GetComponent<FileCover>().SwipeTopTimer();
                }
                else
                {		// If doesnt enough money
                    GameObject WarnMsg = Resources.Load("Prefaps/UI/StaticUI/Warn/WarnMessageLabel") as GameObject;
                    (Instantiate(WarnMsg) as GameObject).transform.parent = transform.parent;
                }
            }
            else if (GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum != 6 && transform.childCount == 1)
            { // Choose Class
                (Instantiate(Check) as GameObject).transform.parent = transform;
                transform.FindChild("Check(Clone)").GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
                transform.FindChild("Check(Clone)").transform.localPosition = new Vector3(1.25f, 2.5f);
                transform.FindChild("Check(Clone)").transform.localScale = Vector3.one;

                (Instantiate(FileCover) as GameObject).transform.parent = transform.parent;
                GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().bSwipeToTop = true;

                (Instantiate(classArrowPrefab) as GameObject).transform.parent = transform.parent;
                GameObject.Find("ClassArrows(Clone)").transform.localPosition = new Vector3(2.15f, 15);
                GameObject.Find("ClassArrows(Clone)").transform.localScale = new Vector3(0.1f, 0.1f);
                GameObject.Find("ClassArrows(Clone)").transform.GetChild(0).GetComponent<UISprite>().depth = 72;
                GameObject.Find("ClassArrows(Clone)").transform.GetChild(0).GetComponent<ClassArrow>().bRightArrow = true;
                GameObject.Find("ClassArrows(Clone)").transform.GetChild(1).GetComponent<UISprite>().depth = 72;
                GameObject.Find("ClassArrows(Clone)").transform.GetChild(1).GetComponent<ClassArrow>().bRightArrow = false;
            }
        }
    }

    public void ButtonStatus(bool bStatus)
    {
        bBtnDisabled = bStatus ? false : true;
    }
}
